﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;


namespace Donate.DAl
{
    public class DonatesEntity
    {
        public Int32 Id { set; get; }
        public string Nameof { set; get; }
        public DateTime Dob { set; get; }
        public string Type { set; get; }
        public string Mobile { set; get; }
        public string Phone { set; get; }
        public string Mobile2 { set; get; }
        public DateTime LastDonDate { set; get; }
        public string CanDonate { set; get; }
        public string Notes { set; get; }
        public string Supervivor { set; get; }
        public string Gender { set; get; }

    }
    public class Donates
    {
       // private static string connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='G:\بنك الدم\Donate\Donate\bin\Debug\Donates.accdb' ;Jet OLEDB:Database password =Yarabe11 ";
        private static string connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + (System.Reflection.Assembly.GetExecutingAssembly().Location).Remove((System.Reflection.Assembly.GetExecutingAssembly().Location).IndexOf("Donate.exe")) + "Donates.accdb;Jet OLEDB:Database Password=Yarabe11";
        private static string cmdtext = "";

        public static bool Insert(DonatesEntity d)
        {
            bool flag = true;

            cmdtext = "INSERT INTO Donates (Nameof,Dob,Type,Mobile,Phone,Mobile2,LastDonDate,CanDonate,Notes,Supervivor,Gender) values (?,?,?,?,?,?,?,?,?,?,?)";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdtext,con);

            cmd.Parameters.AddWithValue("@Nameof",d.Nameof);
            cmd.Parameters.AddWithValue("@Dob",d.Dob);
            cmd.Parameters.AddWithValue("@Type", d.Type);
            cmd.Parameters.AddWithValue("@Mobile",d.Mobile);
            cmd.Parameters.AddWithValue("@Phone",d.Phone);
            cmd.Parameters.AddWithValue("@Mobile2",d.Mobile2);
            cmd.Parameters.AddWithValue("@LastDonDate",d.LastDonDate);
            cmd.Parameters.AddWithValue("@CanDonate",d.CanDonate);
            cmd.Parameters.AddWithValue("@Notes",d.Notes);
            cmd.Parameters.AddWithValue("@Supervivor",d.Supervivor);
            cmd.Parameters.AddWithValue("@Gender", d.Gender);
            try
            {
                con.Open();
                Int32 x = cmd.ExecuteNonQuery();
                if (x == 0)
                {
                    flag = false;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error In Insert"+ e.Message);
                flag = false;
            }
            finally 
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return flag;

        }
        public static Boolean Update(DonatesEntity d)
        {
            bool flag = true;
            cmdtext = "Update Donates set Nameof=? , Dob=? , Type=? , Mobile=? , Phone=? , Mobile2=? , LastDonDate=? , CanDonate=? , Notes=? , supervivor=? , Gender=?  where Id=?";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdtext, con);
           
            cmd.Parameters.AddWithValue("@Nameof",d.Nameof);
            cmd.Parameters.AddWithValue("@Dob",d.Dob);
            cmd.Parameters.AddWithValue("@Type",d.Type);
            cmd.Parameters.AddWithValue("@Mobile",d.Mobile);
            cmd.Parameters.AddWithValue("@Phone",d.Phone);
            cmd.Parameters.AddWithValue("@Mobile2",d.Mobile2);
            cmd.Parameters.AddWithValue("@LastDonDate",d.LastDonDate);
            cmd.Parameters.AddWithValue("@CanDonate",d.CanDonate);
            cmd.Parameters.AddWithValue("@Notes",d.Notes);
            cmd.Parameters.AddWithValue("@supervivor",d.Supervivor);
            cmd.Parameters.AddWithValue("@Gender",d.Gender);
            cmd.Parameters.AddWithValue("@Id", d.Id);
            try
            {
                con.Open();
                Int32 x = cmd.ExecuteNonQuery();
                if (x == 0)
                {
                    flag = false;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error in Update"+ e.Message);
                flag = false;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
                
            }
            return flag;
        }
        public static Boolean Delete(Int32 i)
        {
            bool flag = true;
            cmdtext = "delete from Donates where Id= ?  ";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdtext, con);

            cmd.Parameters.AddWithValue("@Id",i);
            try
            {
                con.Open();
                Int32 x = cmd.ExecuteNonQuery();
                if (x == 0)
                {
                    flag = false;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error In Delete", e.Message);
                flag = false;
            }
            finally
            {
                if(con.State==ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return flag;
        }
        public static DonatesEntity GetById(int id)
        {
            DonatesEntity d = null;
            cmdtext = "Select * from Donates where Id = ?";
            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdtext, con);
            cmd.Parameters.AddWithValue(@"Id", id);

            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                d = FillEntityFromReader(reader);
                reader.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("DonateEntity Error" + e.Message);
            }
            finally 
            {
                if(con.State==ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return d;

        }
        
      
        public static List< DonatesEntity> GetByName(string name)
        {
            List<DonatesEntity> ListEntity = new List<DonatesEntity>();
            DonatesEntity d = null;
            cmdtext = "Select * from Donates where Nameof Like ?";
            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdtext, con);
            cmd.Parameters.AddWithValue(@"Nameof", name);

            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
              while(reader.Read())
              {
                  d = FillEntityFromReader(reader);
                  ListEntity.Add(d);
              }
            }
            catch (Exception e)
            {
                MessageBox.Show("GetByname " + e.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return ListEntity;

        }
        public static List<DonatesEntity> GetAll()
        {
            DonatesEntity d;
            List<DonatesEntity> Donates = new List<DonatesEntity>();
            cmdtext = "Select * from Donates";
            OleDbConnection  con = new OleDbConnection (connstring);
            OleDbCommand cmd = new OleDbCommand(cmdtext ,con);

            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    d = FillEntityFromReader(reader);
                    Donates.Add(d);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("DonatesEntity Error" + e.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return Donates;

        }
        public static DonatesEntity FillEntityFromReader(OleDbDataReader reader)
        {
            DonatesEntity d = new DonatesEntity();
            d.Id =Int32.Parse( reader["Id"].ToString());
            if (reader["Nameof"].ToString()!=null)
            d.Nameof =reader["Nameof"].ToString();
            if (reader["Dob"].ToString() != null)
            d.Dob =Convert.ToDateTime(reader["Dob"].ToString());
            if (reader["Type"].ToString() != null)
            d.Type = reader["Type"].ToString();
            if (reader["Mobile"].ToString() != null)
            d.Mobile = reader["Mobile"].ToString();
            if (reader["Phone"].ToString() != null)
            d.Phone = reader["Phone"].ToString();
            if (reader["Mobile2"].ToString() != null)
            d.Mobile2 =reader["Mobile2"].ToString();
            if (reader["LastDonDate"].ToString() != null)
            d.LastDonDate = Convert.ToDateTime(reader["LastDonDate"].ToString());
            if (reader["CanDonate"].ToString() != null)
            d.CanDonate =( reader["CanDonate"].ToString());
            if (reader["Notes"].ToString() != null)
            d.Notes=reader["Notes"].ToString();
            if (reader["Supervivor"].ToString() != null)
            d.Supervivor=reader["Supervivor"].ToString();
            if (reader["Gender"].ToString()!=null)
            d.Gender=reader["Gender"].ToString();

            return d;
        }
    }
}

